﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.BizFacade
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    using Boiling.Common;
    using Boiling.DataAccess;

    public class c_Diary
    {
        public String diaryID;
        public String diaryOwnerID;
        public String diaryTitle;
        public String diartContent;
        public String diaryDate;
    }



    /// <summary>
    /// 处理用户日志的相关信息
    /// </summary>
    public class BlogManager
    {
        
        /// <summary>
        /// 通过用户邮箱返回日志列表
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns></returns>
        public b_Diarydata GetDiaryByID(String hostID)
        {
            return (new b_Diary()).getdiarybyID(hostID);
        }

        /// <summary>
        /// 通过用户邮箱返回日志id列表数组
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns>日志di列表数组</returns>
        public String[] GetDiaryIDbyUserID(String hostID)
        {
            int n = GetDiaryNumberbyID(hostID);
            String[] res = new String[n];
            for (int i = 0; i < n; i++ )
            {
                res[i] = (new BlogManager()).GetDiaryByID(hostID).Tables[b_Diarydata.b_Diary_Table].Rows[i][b_Diarydata.Diary_ID_Field].ToString();
            }
            return res;
        }

        /// <summary>
        /// 通过用户邮箱返回该用户的所有日志标题
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns>存放用户日志标题的字符串数组</returns>
        public String[] GetDiaryTitleByUserID(String hostID)
        {
            int n = GetDiaryNumberbyID(hostID);
            String[] diarytitle = new String[n];
            
            for (int i = 0; i < n; i++ )
            {
                diarytitle[i] = (new BlogManager()).GetDiaryByID(hostID).Tables[b_Diarydata.b_Diary_Table].Rows[i][b_Diarydata.Diary_Title_Field].ToString();
            }
            return diarytitle;
        }



        /// <summary>
        /// 由日志id返回日志
        /// </summary>
        /// <param name="diayrId">日志标号</param>
        /// <returns></returns>
        public b_Diarydata GetDiaryByNo(int diaryId)
        {
            b_Diarydata diarydata = (new b_Diary()).getdiarybyNO(diaryId);
            if (diarydata.Tables[b_Diarydata.b_Diary_Table].Rows.Count == 0)
                return null;
            else
                return diarydata;
        }


        /// <summary>
        /// 通过日志id返回日志的封装类
        /// </summary>
        /// <param name="diaryID">日志id</param>
        /// <returns>日志封装类</returns>
        public c_Diary GetDiarydataByNo(String s_diaryID)
        {
            int diaryID = Int32.Parse(s_diaryID);
            c_Diary diary = new c_Diary();
            b_Diarydata diarydata = new b_Diarydata();
            diarydata = (new BlogManager()).GetDiaryByNo(diaryID);
            if (diarydata == null)
                return null;

            diary.diaryID = diarydata.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.Diary_ID_Field].ToString();
            diary.diaryOwnerID = diarydata.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.User_ID_Field].ToString();
            diary.diaryDate = diarydata.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.Diary_date_Field].ToString();
            diary.diaryTitle = diarydata.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.Diary_Title_Field].ToString();
            diary.diartContent = diarydata.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.Diary_Content_Field].ToString();

            return diary;

        }



        /// <summary>
        /// 通过用户邮箱计算用户的日志总数
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns></returns>
        public int GetDiaryNumberbyID(String hostID)
        {
            return (new b_Diary()).countDiarybyID(hostID);
        }

        /// <summary>
        /// 通过日志id删除该日志
        /// </summary>
        /// <param name="diaryId">日志id</param>
        public bool DeleteDiaryByNo(String diaryID)
        {
            int diaryid = Int32.Parse(diaryID);
            return (new b_Diary()).deleteDiarybyno(diaryid);
        }

        /// <summary>
        /// 通过用户注册邮箱删除该用户的所有日志【慎用！】
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns></returns>
        public bool DeleteDiaryByID(String hostID)
        {
            return (new b_Diary()).deleteDiarybyID(hostID);
        }

        /// <summary>
        /// 修改日志内容
        /// </summary>
        /// <param name="hostID">日志主人id</param>
        /// <param name="diaryTitle">日志标题</param>
        /// <param name="diaryContent">日志内容</param>
        /// <returns></returns>
        public bool EditDiary(String hostID, String diaryTitle, String diaryContent, String diaryid)
        {
            b_Diarydata diaryData = new b_Diarydata();
            DataTable table = diaryData.Tables[b_Diarydata.b_Diary_Table];
            DataRow row = table.NewRow();

            row[b_Diarydata.Diary_Content_Field] = diaryContent;
            row[b_Diarydata.Diary_date_Field] = System.DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒");
            row[b_Diarydata.User_ID_Field] = hostID;
            row[b_Diarydata.Diary_Title_Field] = diaryTitle;
            row[b_Diarydata.Diary_ID_Field] = Int32.Parse(diaryid);

            table.Rows.Add(row);
                 
            return (new b_Diary()).modifydiary(diaryData);
        }


        /// <summary>
        /// 增加新日志
        /// </summary>
        /// <param name="hostID">日志主人id</param>
        /// <param name="diaryTitle">日志标题</param>
        /// <param name="diaryContent">日志内容</param>
        /// <returns></returns>
        public bool InsertDiary(String hostID, String diaryTitle, String diaryContent)
        {
            b_Diarydata diaryData = new b_Diarydata();
            DataTable table = diaryData.Tables[b_Diarydata.b_Diary_Table];
            DataRow row = table.NewRow();

            row[b_Diarydata.Diary_Content_Field] = diaryContent;
            row[b_Diarydata.Diary_date_Field] = System.DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒");
            row[b_Diarydata.User_ID_Field] = hostID;
            row[b_Diarydata.Diary_Title_Field] = diaryTitle;

            table.Rows.Add(row);

            bool test = (new b_Diary()).insertdiary(diaryData);
            b_Diarydata newdata = new b_Diary().getdiarybyID(hostID);
            int rowscount = newdata.Tables["b_Diary"].Rows.Count;
            diaryData.Tables["b_Diary"].Rows[0]["Diary_ID"] = newdata.Tables["b_Diary"].Rows[rowscount - 1]["Diary_Id"];

            return (new NewsManager()).InsertNewofDiaryToAllFriends(diaryData);           
        }


        //inc the counter of the diary visitors
        public void IncVisitorCount()
        {

        }

        //get the counter of the diary visitors
        public void GetVisitorCount()
        {

        }
    }
}
